package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.generator.entity.CouponEntity;
import io.renren.modules.generator.service.CouponService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 优惠券列表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-04-16 23:24:56
 */
@RestController
@RequestMapping("generator/coupon")
@Api("优惠券")
public class CouponController {
    @Autowired
    private CouponService couponService;

    /**
     * 列表
     */
    @ApiOperation("优惠券列表")
    @GetMapping("/list")
    @RequiresPermissions("generator:coupon:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = couponService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 获取单个用户优惠券
     */
    @ApiOperation("获取单个用户优惠券")
    @GetMapping("/info/{userId}")
    @RequiresPermissions("generator:coupon:info")
    public R info(@PathVariable("userId") Long userId, @RequestParam Map<String, Object> params){
        PageUtils page = couponService.queryPageByUserId(userId, params);

        return R.ok().put("page", page);
    }

    /**
     * 保存
     */
    @ApiOperation("单派券/用户买券都用这个接口,couponType为1时单派券,为2时用户买券")
    @PostMapping("/save")
    @RequiresPermissions("generator:coupon:save")
    public R save(@RequestBody CouponEntity coupon){
		couponService.save(coupon);

        return R.ok();
    }

    /**
     * 批量派发优惠券
     */
    @ApiOperation("批量派发优惠券，除了userId，其他都需要传")
    @PostMapping("/batch")
    @RequiresPermissions("generator:coupon:save")
    public R batch(@RequestBody CouponEntity coupon){
        couponService.batchCoupon(coupon);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改优惠券信息")
    @PostMapping("/update")
    @RequiresPermissions("generator:coupon:update")
    public R update(@RequestBody CouponEntity coupon){
		couponService.updateById(coupon);

        return R.ok();
    }

    /**
     * 使用优惠券
     */
    @ApiOperation("使用优惠券,可以只传优惠券id")
    @PostMapping("/use")
    @RequiresPermissions("generator:coupon:update")
    public R use(@RequestBody CouponEntity coupon){
        couponService.removeById(coupon.getId());

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("generator:coupon:delete")
    public R delete(@RequestBody Integer[] ids){
        couponService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
